package com.hsy.directseeding.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.deparse.dpframe.ui.activity.DPBaseActivity;
import com.deparse.dpframe.utils.DPCommonUtil;
import com.deparse.dpframe.utils.DPUnitUtil;
import com.hsy.directseeding.R;
import com.hsy.directseeding.account.LoginActivity;
import com.hsy.directseeding.common.Config;
import com.hsy.directseeding.common.Consts;
import com.hsy.directseeding.common.LoadingProgressDialog;

import java.util.List;

/**
 * Created by hsy on 2016/3/23.
 */
public class CommonUtils {
    public static final int TYPE_FROM_CAMERA = 0;
    public static final int TYPE_FROM_ALBUM = 1;
    private static final String INVALID_IM_TOKEN = "INVALID_IM_TOKEN#$@#@%$@!#^#@!#!#%@#$@!#%INVALID_IM_TOKEN";
    private static final String INVALID_IM_ID = "INVALID_IM_ID#$@#@%$@!#^#@!#!#%@#$@!#%INVALID_IM_ID";
    public static String INVALID_TOKEN = "-1";

    public static final String INVALID_UID = "INVALID_UID";

    public static void saveToken(Context context, String token) {
        context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_APPEND).edit().putString(Consts.KEY_TOKEN, token).commit();
    }

    public static String getToken(Context context) {
        return context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_APPEND).getString(Consts.KEY_TOKEN, INVALID_TOKEN);
    }

    public static void clearToken(Context context) {
        context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_APPEND).edit().remove(Consts.KEY_TOKEN).commit();
    }

    public static boolean checkLogin(Context context) {
        String token = getToken(context);
        if (INVALID_TOKEN.equals(token)) {
            return false;
        }
        return true;
    }

    private static Dialog mProgressDialog = null;

    public static void showProgressDialog(Context context) {
//        DPLogUtil.printStackTrace("printStackTrace");
        if (null == mProgressDialog || !mProgressDialog.isShowing()) {
            mProgressDialog = new LoadingProgressDialog(context);
        }
        mProgressDialog.show();
    }

    public static void hideProgressDialog() {
        if (null != mProgressDialog) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    public static boolean isProgressDialogShowing() {
        return null != mProgressDialog && mProgressDialog.isShowing();
    }

    private static Dialog authFailureDialog;

    public static boolean isAuthDialogShowing() {
        return null != authFailureDialog && authFailureDialog.isShowing();
    }


    public static void showUploadProgressDialog(Context context) {
        DPCommonUtil.showUploadProgressDialog(context, "");
    }

    public static void hideUploadProgressDialog() {
        DPCommonUtil.hideUploadProgressDialog();
    }

    public interface ImageSelectDialogDismissListener {
        boolean onDismiss(int type);
    }

    public interface IDetailActionBarInitListener {
        void onInit(ImageView shareButton, ImageView collectButton, TextView collectNumTextView);
    }

    public static void showAuthFailureDialog(final Context activityContext, final String msg) {
        hideProgressDialog();
        LinearLayout layout = new LinearLayout(activityContext);
        layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        TextView textView = new TextView(activityContext);
        textView.setText(msg);
        textView.setTextAppearance(activityContext, R.style.style_auth_err_msg);

        int padding = DPUnitUtil.dip2px(activityContext, 10);
        textView.setPadding(padding, padding, padding, padding);

        layout.addView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        final AlertDialog.Builder builder = new AlertDialog.Builder(activityContext);
        builder.setView(layout);
        builder.setPositiveButton("确认",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        activityContext.startActivity(new Intent(activityContext, LoginActivity.class));
                    }
                });

        authFailureDialog = builder.create();
        authFailureDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                CommonUtils.clearToken(activityContext);
                authFailureDialog = null;
                activityContext.startActivity(new Intent(activityContext, LoginActivity.class));
            }
        });
        authFailureDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        authFailureDialog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (null != authFailureDialog) {
                    authFailureDialog.dismiss();
                }
            }
        }, 5000);
    }

    public static boolean checkLog(DPBaseActivity context, List<EditText> editText, List<String> msg) {


        for (int i = 0; i < editText.size(); i++) {
            if (isEmpty(editText.get(i).getText().toString())) {
                context.showToast(msg.get(i));
                return false;
            }

        }
        return true;
    }

    private static boolean isEmpty(String src) {
        return null == src || "".equals(src);
    }
}
